﻿using System;
using System.Windows;
using System.Linq;
#if WPF
using System.Windows.Media;
using System.Windows.Shapes;
#else
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.Foundation;
using Windows.UI.Xaml.Data;
#endif

namespace Eternal.DataVisualization
{
    public class PieSeries : NonAxisType
    {
        protected internal override void OnDrawSegment()
        {
            base.OnDrawSegment();
            SegmentInfos.Clear();
            SegmentPanel.Children.Clear();
            double dataCount = ActualXData.Count;
            var sum = ActualYData.Sum();
            var halfCircle = HalfCircle;
            var sa = halfCircle ? 90 * (Math.PI / 2) : 0;

            for (var i = 0; i < dataCount; i++)
            {
                var yValue = ActualYData[i];

                var ea = Math.Abs(yValue) * (Math.PI * 2 / sum) + sa;
                    
                Point origin;
                double radius;
                if (halfCircle)
                {
                    origin = new Point(AvailableWidth / 2, AvailableHeight);
                    radius = Math.Min(AvailableHeight, AvailableWidth / 2);
                }
                else
                {
                    origin = new Point(AvailableWidth / 2, AvailableHeight / 2);
                    radius = Math.Min(AvailableHeight, AvailableWidth) / 2;
                }

                var pathFigure = new PathFigure {StartPoint = origin};
                var line = new LineSegment
                {
                    Point = new Point(origin.X + radius * Math.Cos(sa), origin.Y + radius * Math.Sin(sa))
                };

                pathFigure.Segments.Add(line);

                var argSegment = new ArcSegment
                {
                    Point = new Point(origin.X + radius * Math.Cos(ea), origin.Y + radius * Math.Sin(ea)),
                    Size = new Size(radius, radius),
                    RotationAngle = ea + sa,
                    IsLargeArc = ea - sa > Math.PI,
                    SweepDirection = SweepDirection.Clockwise
                };

                pathFigure.Segments.Add(argSegment);
                pathFigure.IsClosed = true;
                var geo = new PathGeometry {Figures = new PathFigureCollection {pathFigure}};

                var path = new Path {Fill = Area.GetBrush(i), Data = geo};
                SegmentPanel.Children.Add(path);

                var info = new SegmentInfo {Value = new Point(i, yValue)};
                var centerAngle = sa + (ea - sa) / 2;
                var labelRadius = (radius * LabelRadius) / 100;
                info.Point = new Point(origin.X + labelRadius * Math.Cos(centerAngle), origin.Y + labelRadius * Math.Sin(centerAngle));
                info.Series = this;
                path.Tag = info;
                if (Label != null)
                {
                    Label.Positions.Add(info);
                }

                sa += (ea - sa);
            }
        }

        Path previousElement;
#if WPF
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
#else
        protected override void OnPointerMoved(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
#endif
        {
            if (previousElement != null)
            {
                var brush = ((SolidColorBrush) previousElement.Fill).Color;
                previousElement.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R / 0.6), (byte)(brush.G / 0.6), (byte)(brush.B / 0.6)));
                previousElement.StrokeThickness = 0;
                previousElement = null;
            }
            var rect = e.OriginalSource as Path;

            if (rect != null)
            {
                var brush = ((SolidColorBrush) rect.Fill).Color;
                rect.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R * 0.6), (byte)(brush.G * 0.6), (byte)(brush.B * 0.6)));
                previousElement = rect;
            }
#if WPF
            base.OnMouseMove(e);
#else
            base.OnPointerMoved(e);
#endif
        }
    }
}
